#! node
const minimist = require("minimist");
const ajax = require("./http.js");
const loading = require("./loading");
const edition = require("./package.json").version;

const start = async () => {
  const args = minimist(process.argv.slice(2)); //前两个是编译器相关路径信息，可以忽略
  let cmd = args._[0] || "help";
  if (args.v || args.version) {
    cmd = "version"; //查询版本优先！
  }
  let location = args.location || "beijing";
  loading.start();
  let data = await ajax(location);
  data = data.results[0];
  let posotion = data.location;
  let daily = data.daily;

  switch (cmd) {
    case "today":
      console.log(
        `\t\x1B[32m${posotion.timezone_offset}时区   ${posotion.name}天气  ${posotion.country} \x1B[39m\n`
      );

      console.log(
        `\t\x1B[32m${daily[0].date}（今天）  白天：${daily[0].text_day}   夜晚：${daily[0].text_night} \x1B[39m`
      );
      loading.stop();
      break;
    case "version":
      console.log("---version---", edition);
      loading.stop();
      break;
    case "help":
      console.log(`
          weather [command] <options>
          today .............. show weather for today
          tomorrow ............show weather for tomorrow
          version ............ show package version
          help ............... show help menu for a command
        `);
      loading.stop();
  }
};

start();

module.exports = start;
